﻿using UnityEngine;
using System.Collections;

public enum EezingMethod
{
    none,
    toLeft,
    toRight,
    toUp,
    toDown
};

public class Eezing : MonoBehaviour 
{
    private bool EezingOn = false;

    public void EezIn(EezingMethod method, GameObject gameObjectt, Vector3 finalPos, float time, float startLocDiff)
    {
        switch (method)
        {
            case EezingMethod.none:
                break;

            case EezingMethod.toLeft:
                Vector3 endLocL = finalPos;
                Vector3 startLocL = endLocL;
                startLocL.x += startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocL, endLocL, time));              
                break;

            case EezingMethod.toRight:
                Vector3 endLocR = finalPos;
                Vector3 startLocR = endLocR;
                startLocR.x -= startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocR, endLocR, time));               
                break;

            case EezingMethod.toUp:
                Vector3 endLocU = finalPos;
                Vector3 startLocU = endLocU;
                startLocU.y -= startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocU, endLocU, time));
                break;

            case EezingMethod.toDown:
                Vector3 endLocD = finalPos;
                Vector3 startLocD = endLocD;
                startLocD.y += startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocD, endLocD, time));
                break;

            default:
                Debug.Log("Error Eezing method", this);
                break;
        }
    }

    private IEnumerator Eez (GameObject gameObjectt, Vector3 startLoc, Vector3 endLoc, float time)
    {
        EezingOn = true;
        float t = 0;
        while (t / time < 1)
        {
            t += Time.deltaTime;
            gameObjectt.transform.position = Vector3.Lerp(startLoc, endLoc, t / time);
            yield return null;
        }
        EezingOn = false;
    }

    public void EezOut(EezingMethod method, GameObject gameObjectt, Vector3 finalPos, float time, float startLocDiff)
    {
        switch (method)
        {
            case EezingMethod.none:
                break;

            case EezingMethod.toLeft:
                Vector3 startLocL = finalPos;
                Vector3 endLocL = startLocL;
                endLocL.x -= startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocL, endLocL, time));              
                break;

            case EezingMethod.toRight:
                Vector3 startLocR = finalPos;
                Vector3 endLocR = startLocR;
                endLocR.x += startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocR, endLocR, time));
                break;

            case EezingMethod.toUp:
                Vector3 startLocU = finalPos;
                Vector3 endLocU = startLocU;
                endLocU.y += startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocU, endLocU, time));
                break;

            case EezingMethod.toDown:
                Vector3 startLocD = finalPos;
                Vector3 endLocD = startLocD;
                endLocD.y -= startLocDiff;
                StartCoroutine(Eez(gameObjectt, startLocD, endLocD, time));
                break;

            default:
                Debug.Log("Error Eezing method", this);
                break;
        }
    }

    public bool IsEezingOn()
    {
        return EezingOn;
    }
}


